package myky.com.doctorapp.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_health_artical.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.interface_.JsApi

class HealthArticalActivity : AppCompatActivity() {

    var loadUrl: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_health_artical)

        initViews()
        initDatas()
    }

    private fun initViews() {

        ImmersionBar.with(this).barColorInt(resources.getColor(R.color.colorPrimary)).titleBar(R.id.titleLayout).navigationBarEnable(false).init()

        backLayout.setOnClickListener { finish() }

    }

    private fun initDatas() {

        val articleid = intent.getIntExtra("Articleid", 0)
        val title = intent.getStringExtra("Author")
        if (title != null) {
            conmonTitleTextView.text = title


        } else {

            conmonTitleTextView.text = "内容详情"
        }

        loadUrl = Address.HealthContentUrl + articleid
        when (articleid) {
            101 -> loadUrl = intent.getStringExtra("UserUpdateUrl")
        }
        loadWebShow()
    }

    private fun loadWebShow() {
        web_doctor_pb.max = 100
        act_banner_webview.isHorizontalScrollBarEnabled = false//水平不显示
        act_banner_webview.isVerticalScrollBarEnabled = false //垂直不显示
        act_banner_webview.settings.setSupportZoom(true)
        act_banner_webview.settings.builtInZoomControls = true
        // loadUrl = "http://10.100.1.221:8081/#/xh_teaminfo";
        act_banner_webview.addJavascriptObject(JsApi(), null)
        act_banner_webview.webChromeClient = WebProgressClient()
        act_banner_webview.webViewClient = object : WebViewClient() {
        }
        act_banner_webview.loadUrl(loadUrl)
    }

    //加载进度回调
    private inner class WebProgressClient : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            web_doctor_pb.progress = newProgress
            if (newProgress == 100) {
                web_doctor_pb.visibility = View.GONE
            }
            super.onProgressChanged(view, newProgress)
        }
    }

}
